package org.bonorsoft.modules.control.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.wechat.entity.NywxHf;
import org.bonorsoft.modules.wechat.entity.NywxTj;
import org.bonorsoft.modules.wechat.entity.WxRequest;
import org.bonorsoft.modules.wechat.service.INywxHfService;
import org.bonorsoft.modules.wechat.service.INywxPhotoService;
import org.bonorsoft.modules.wechat.service.INywxTjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Slf4j
@Controller
@RequestMapping("/control/nywx")
public class NywxTjController extends BonorController<NywxTj, INywxTjService> {
    private String prefix = "control/nywx";

    @Autowired
    private INywxTjService nywxTjService;
    @Autowired
    private INywxHfService nywxHfService;
    @Autowired
    private INywxPhotoService nywxPhotoService;

    /**
     * 你有我需 首页
     */
    @RequiresPermissions("control:nywx:view")
    @GetMapping()
    public String index() {
        return prefix + "/nywx";
    }

    /**
     * 你有我需 分页列表查询
     *
     * @param nywx
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "你有我需-分页列表查询")
    @RequiresPermissions("control:nywx:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(NywxTj nywx,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<NywxTj> queryWrapper = QueryGenerator.initQueryWrapper(nywx, req.getParameterMap());
        Page<NywxTj> page = new Page<NywxTj>(pageNo, pageSize);
        IPage<NywxTj> pageList = nywxTjService.paging(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 你有我需 我有 添加页面
     */
    @GetMapping("/iHaveAdd")
    public String iHaveAdd(ModelMap map) {
        map.put("nywxlb", "0");
        return prefix + "/add";
    }

    /**
     * 你有我需 我需 添加页面
     */
    @GetMapping("/iNeedAdd")
    public String iNeedAdd(ModelMap map) {
        map.put("nywxlb", "1");
        return prefix + "/add";
    }

    /**
     * 你有我需 添加
     *
     * @param nywxTj
     * @return
     */
    @AutoLog(value = "你有我需-添加")
    @RequiresPermissions("control:nywx:add")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result<?> add(@Validated NywxTj nywxTj, String photo) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        String qqid = UUID.randomUUID().toString().replaceAll("-", "");
        nywxTj.setTjid(qqid).setTjrid(user.getUsername()).setTjrxm(user.getRealname()).setTjsj(new Date());
        nywxTjService.save(nywxTj);
        if (oConvertUtils.isNotEmpty(photo)) {
            String[] split;
            if (photo.contains(",")) {
                split = photo.split(",");
            } else {
                split = new String[]{photo};
            }
            for (String s : split) {
                nywxPhotoService.addPhoto(qqid, s);
            }
        }
        return Result.ok("添加成功！");
    }

    /**
     * 你有我需 回复
     */
    @GetMapping("/hfAdd/{tjid}")
    public String hfAdd(@PathVariable("tjid") String tjid, ModelMap map){
        NywxTj nywxTj = nywxTjService.getById(tjid);
        map.put("nywxTj",nywxTj);
        return prefix + "/hfAdd";
    }

    /**
     * 你有我需 回复
     *
     * @param nywxHf
     * @return
     */
    @AutoLog(value = "你有我需-回复")
    @RequiresPermissions("control:nywx:hfAdd")
    @PostMapping(value = "/hfAdd")
    @ResponseBody
    public Result<?> hfAdd(@Validated NywxHf nywxHf) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        String hfid = UUID.randomUUID().toString().replaceAll("-", "");
        nywxHf.setHfid(hfid).setHfrid(user.getUsername()).setHfrxm(user.getRealname()).setHfsj(new Date());
        nywxHfService.save(nywxHf);
        return Result.ok("添加成功！");
    }

    /**
     * 三维码 你有我需 我有 列表
     */
    @GetMapping("/mobileIHaveList")
    public String iHaveList(ModelMap map) {
        List<NywxTj> iHaveList = nywxTjService.iHaveList();
        map.put("iHaveList", iHaveList);
        return "errorDetail/iHaveList";
    }

    /**
     * 三维码 你有我需 我需 列表
     */
    @GetMapping("/mobileINeedList")
    public String iNeedList(ModelMap map) {
        List<NywxTj> iNeedList = nywxTjService.iNeedList();
        map.put("iNeedList", iNeedList);
        return "errorDetail/iNeedList";
    }

    /**
     * 三维码 你有我需 详细
     */
    @GetMapping("/mobileDetail/{id}")
    public String mobileDetail(@PathVariable("id") String id, ModelMap map) {
        map.put("nywx", nywxTjService.getById(id));
        map.put("photo", nywxPhotoService.getPhoto(id));
        return "errorDetail/nywxDetails";
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "你有我需-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        nywxTjService.removeById(id);
        nywxPhotoService.deletePhoto(id);
        nywxHfService.removeByTjid(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "呼应-批量删除")
    @RequiresPermissions("control:nywx:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    @ResponseBody
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        nywxTjService.removeByIds(list);
        nywxPhotoService.deletePhotoByIds(list);
        for (String tjid : list) {
            nywxHfService.removeByTjid(tjid);
        }
        return Result.ok("删除成功!");
    }


}
